/**
 * 
 */
package downloader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author balage
 *
 */
public class CacheInputStream extends InputStream {

	private final FileOutputStream cache;
	private final InputStream is;
	
	/**
	 * @throws FileNotFoundException 
	 * 
	 */
	public CacheInputStream(File cache, InputStream is) throws FileNotFoundException {
		this.cache = new FileOutputStream(cache);
		this.is = is;
	}

	/* (non-Javadoc)
	 * @see java.io.InputStream#read()
	 */
	@Override
	public int read() throws IOException {
		int i = is.read();
		cache.write(i);
		return i;
	}

	@Override
	public void close() throws IOException {
		is.close();
		cache.close();
		super.close();
	}
	
}
